package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.FileSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CategoryMapper;
import com.ruoyi.system.domain.Category;
import com.ruoyi.system.service.ICategoryService;

/**
 * 类目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-04
 */
@Service
public class CategoryServiceImpl implements ICategoryService 
{
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private FileSystemMapper fileSystemMapper;

    /**
     * 查询类目
     * 
     * @param id 类目主键
     * @return 类目
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        Category category = categoryMapper.selectCategoryById(id);
        if (category.getPic()!=null){
            category.setPicFile(fileSystemMapper.selectFileSystemById(Long.valueOf(category.getPic())));
        }
        return category;
    }

    /**
     * 查询类目列表
     * 
     * @param category 类目
     * @return 类目
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        category.setParentId(0L);
        List<Category> categories = categoryMapper.selectCategoryList(category);
        categories.forEach(c -> {
            List<Category> categories1 = categoryMapper.selectCategoryListByParentId(c.getId());
            if (categories1.size()>0){
                categories1.forEach(c1 -> {
                    if (c1.getPic()!=null){
                        c1.setPicFile(fileSystemMapper.selectFileSystemById(Long.valueOf(c1.getPic())));
                    }
                });
            }
            c.setChildren(categories1);
            if (c.getPic()!=null){
                c.setPicFile(fileSystemMapper.selectFileSystemById(Long.valueOf(c.getPic())));
            }

        });
        return categories;
    }

    /**
     * 新增类目
     * 
     * @param category 类目
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        category.setCreateTime(DateUtils.getNowDate());
        if (category.getPicFile()!=null){
            category.setPic(category.getPicFile().getId().toString());
        }
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改类目
     * 
     * @param category 类目
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        category.setUpdateTime(DateUtils.getNowDate());
        if (category.getPicFile()!=null){
            category.setPic(category.getPicFile().getId().toString());
        }
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除类目
     * 
     * @param ids 需要删除的类目主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除类目信息
     * 
     * @param id 类目主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    @Override
    public List<Category> selectCategoryApiList(Category category) {
        category.setParentId(0L);
        List<Category> categories = categoryMapper.selectCategoryList(category);
        categories.forEach(c -> {
            List<Category> categories1 = categoryMapper.selectCategoryListByApiParentId(c.getId());
            if (categories1.size()>0){
                categories1.forEach(c1 -> {
                    if (c1.getPic()!=null){
                        c1.setPicFile(fileSystemMapper.selectFileSystemById(Long.valueOf(c1.getPic())));
                    }
                });
            }
            c.setChildren(categories1);


        });
        return categories;
    }
}
